<?php namespace App\Core\Repository\Criterias;

use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
use App\Core\Exceptions\RuntimeError;

class InCriteria extends Criteria {
    
    protected $field;
    protected $value;

    public function __construct($field, $value) {
        if(!is_array($value)){
            throw new RuntimeError('InCriteria error.Value mast be an Array.');
        }
        if(count($value) < 1){
            throw new RuntimeError('InCriteria error.Value mast not be empty.');
        }
        $this->field = $field;
        $this->value = $value;
        $this->setName('in_'.$this->field);
    }

    public function apply(Repository $repository){
        parent::apply($repository);
        $this->query->whereIn($this->formatField($this->model, $this->field), $this->value);
    }
}